package org.jeecg.modules.exPingbiao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.BooleanUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.exPingbiao.domain.vo.ExpertRecommendConfigVO;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpertRecommend;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoSectionExpertConfig;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoExpertRecommendMapper;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoSectionExpertConfigMapper;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoSectionExpertConfigService;
import org.jeecg.modules.exPingbiao.vo.ExPingbiaoExpertRecommendVO;
import org.jeecg.modules.exPingbiao.vo.ExPingbiaoExpertVO;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 评标阶段
 * @Author: jeecg-boot
 * @Date: 2024-11-09
 * @Version: V1.0
 */
@Service
public class ExPingbiaoSectionExpertConfigServiceImpl extends ServiceImpl<ExPingbiaoSectionExpertConfigMapper, ExPingbiaoSectionExpertConfig> implements IExPingbiaoSectionExpertConfigService {

    @Autowired
    private ExPingbiaoSectionExpertConfigMapper expertConfigMapper;
    @Autowired
    private ExPingbiaoExpertRecommendMapper expertRecommendMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    @Lazy
    private IExPingbiaoSectionExpertConfigService sectionExpertConfigService;

    @Override
    public Result<List<ExPingbiaoExpertVO>> queryExpertList(String sectionId) {
        List<ExPingbiaoSectionExpertConfig> expertConfigList = expertConfigMapper.selectByProjectAndSectionId(null, sectionId);
        if (CollectionUtils.isEmpty(expertConfigList)) {
            return Result.ok(Lists.newArrayList());
        }
        List<String> expertIdList = expertConfigList.stream().map(ExPingbiaoSectionExpertConfig::getExpertId).collect(Collectors.toList());
        List<SysUser> sysUserList = sysUserMapper.selectBatchIds(expertIdList);
        Map<String, String> sysuserNameMap = sysUserList.stream().collect(Collectors.toMap(SysUser::getId, SysUser::getUsername));

        List<ExPingbiaoExpertVO> expertRecommendList = expertConfigList.stream().map(e -> {
            ExPingbiaoExpertVO recommendVO = new ExPingbiaoExpertVO();
            BeanUtils.copyProperties(e, recommendVO);
            recommendVO.setName(sysuserNameMap.get(e.getExpertId()));
            return recommendVO;
        }).collect(Collectors.toList());
        return Result.ok(expertRecommendList);
    }


    @Override
    public Result<List<ExPingbiaoExpertRecommendVO>> queryRecommendExpertList(String expertId, String sectionId) {
        List<ExPingbiaoSectionExpertConfig> expertConfigList = expertConfigMapper.selectByProjectAndSectionId(null, sectionId);
        if (CollectionUtils.isEmpty(expertConfigList)) {
            return Result.ok(Lists.newArrayList());
        }
        List<String> expertIdList = expertConfigList.stream().map(ExPingbiaoSectionExpertConfig::getExpertId).collect(Collectors.toList());
        List<SysUser> sysUserList = sysUserMapper.selectBatchIds(expertIdList);
        Map<String, String> sysuserNameMap = sysUserList.stream().collect(Collectors.toMap(SysUser::getId, SysUser::getUsername));

        ExPingbiaoExpertRecommend expertRecommend = expertRecommendMapper.selectByExpertIdAndSectionId(expertId, sectionId);


        List<ExPingbiaoExpertRecommendVO> expertRecommendList = expertConfigList.stream().map(e -> {
            ExPingbiaoExpertRecommendVO recommendVO = new ExPingbiaoExpertRecommendVO();
            BeanUtils.copyProperties(e, recommendVO);
            recommendVO.setName(sysuserNameMap.get(e.getExpertId()));
            recommendVO.setRecommendFlag(Boolean.FALSE);
            if (null != expertRecommend) {
                recommendVO.setRecommendFlag(Objects.equals(expertRecommend.getRecommendExpertId(), e.getExpertId()));
            }
            return recommendVO;
        }).collect(Collectors.toList());
        return Result.ok(expertRecommendList);
    }


    @Override
    public Result<ExpertRecommendConfigVO> gqueryExpertRecommendConfig(String expertId, String sectionId) {
        List<ExPingbiaoSectionExpertConfig> expertConfigList = expertConfigMapper.selectByProjectAndSectionId(null, sectionId);
        ExpertRecommendConfigVO expertRecommendConfigVO = new ExpertRecommendConfigVO();
        if (CollectionUtils.isEmpty(expertConfigList)) {
            return Result.ok(expertRecommendConfigVO);
        }
        List<String> expertIdList = expertConfigList.stream().map(ExPingbiaoSectionExpertConfig::getExpertId).collect(Collectors.toList());
        List<SysUser> sysUserList = sysUserMapper.selectBatchIds(expertIdList);
        Map<String, String> sysuserNameMap = sysUserList.stream().collect(Collectors.toMap(SysUser::getId, SysUser::getUsername));

        ExPingbiaoExpertRecommend expertRecommend = expertRecommendMapper.selectByExpertIdAndSectionId(expertId, sectionId);


        List<ExPingbiaoExpertRecommendVO> expertRecommendList = expertConfigList.stream().map(e -> {
            ExPingbiaoExpertRecommendVO recommendVO = new ExPingbiaoExpertRecommendVO();
            BeanUtils.copyProperties(e, recommendVO);
            recommendVO.setName(sysuserNameMap.get(e.getExpertId()));
            recommendVO.setRecommendFlag(Boolean.FALSE);
            if (null != expertRecommend) {
                recommendVO.setRecommendFlag(Objects.equals(expertRecommend.getRecommendExpertId(), e.getExpertId()));
            }
            return recommendVO;
        }).collect(Collectors.toList());
        expertRecommendConfigVO.setExpertList(expertRecommendList);
        expertRecommendConfigVO.setRecommendFlag(null != expertRecommend);
        List<ExPingbiaoSectionExpertConfig> list = expertConfigList.stream().filter(e -> BooleanUtils.isTrue(e.getGroupFlag())).collect(Collectors.toList());
        expertRecommendConfigVO.setGroupFlag(CollectionUtils.isNotEmpty(list));
        return Result.ok(expertRecommendConfigVO);
    }

    @Override
    public Boolean selectSignInfoByUserId(String expertId, String sectionId) {
        ExPingbiaoSectionExpertConfig exPingbiaoExpertSign = expertConfigMapper.selectBySectionIdAndExpertId(sectionId, expertId);
        if (null != exPingbiaoExpertSign) {
            return exPingbiaoExpertSign.getSignFlag();
        }
        return Boolean.FALSE;
    }

    @Override
    public Result<String> sign(String expertId, String sectionId) {
        List<ExPingbiaoSectionExpertConfig> sectionExpertConfigList = expertConfigMapper.selectByProjectAndSectionId(null, sectionId);
        if (CollectionUtils.isEmpty(sectionExpertConfigList)) {
            List<ExPingbiaoSectionExpertConfig> expertConfigList = expertConfigMapper.selectByProjectAndSectionIdIsNull();
            List<ExPingbiaoSectionExpertConfig> insertList = expertConfigList.stream().map(e -> {
                ExPingbiaoSectionExpertConfig expertConfig = new ExPingbiaoSectionExpertConfig();
                expertConfig.setExpertId(e.getExpertId());
                expertConfig.setBidSectionId(sectionId);
                expertConfig.setGroupFlag(Boolean.FALSE);
                expertConfig.setSignFlag(Boolean.FALSE);
                return expertConfig;
            }).collect(Collectors.toList());
            sectionExpertConfigService.saveBatch(insertList);
        }
        ExPingbiaoSectionExpertConfig exPingbiaoExpertSign = expertConfigMapper.selectBySectionIdAndExpertId(sectionId, expertId);
        if (null == exPingbiaoExpertSign) {
            return Result.error("专家记录不存在");
        }
        ExPingbiaoSectionExpertConfig updateEntity = new ExPingbiaoSectionExpertConfig();
        updateEntity.setId(exPingbiaoExpertSign.getId());
        updateEntity.setSignFlag(Boolean.TRUE);
        updateEntity.setSignTime(LocalDateTime.now());
        expertConfigMapper.updateById(updateEntity);
        return Result.ok();
    }
}
